package com.atjg.autocat.main

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.app.ComponentCaller
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.Settings
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.atjg.autocat.databinding.ActivitySplashBinding
import com.blankj.utilcode.util.ActivityUtils
import com.zhy.baselibrary.base.BaseActivity
import com.zhy.baselibrary.base.BaseApp
import com.zhy.baselibrary.base.BaseViewModel
import com.zhy.baselibrary.expand.ToastMgr


@SuppressLint("CustomSplashScreen")
class SplashActivity : BaseActivity<ActivitySplashBinding, BaseViewModel>() {
    private val waitTime: Long = 2000
    private var touchTime: Long = 0

    override fun initialize() {
        if (!isTaskRoot) {
            ActivityUtils.finishActivity(this)
            return
        }
        binding.tvTitle.text = App.projectName
        jumpMain()
//        checkPermission()
    }

    private fun jumpMain() {
        BaseApp.mHandle?.postDelayed({
            ActivityUtils.startActivity(MainActivity::class.java)
        }, 2000)
    }

    override fun onBackPressed() {
        val currentTime = System.currentTimeMillis()
        if (currentTime - touchTime >= waitTime) {
            ToastMgr.show("再按一次退出程序", "warn")
            touchTime = currentTime
            return
        } else {
            ActivityUtils.finishAllActivities()
        }
        super.onBackPressed()
    }

    private fun checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (Environment.isExternalStorageManager()) {
                checkReadCameraPermission()
            } else {
                val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
                intent.data = Uri.parse("package:$packageName")
                startActivityIfNeeded(intent, 1024)
                return
            }
        } else {
            checkReadCameraPermission()
        }
    }

    override fun onActivityResult(
        requestCode: Int,
        resultCode: Int,
        data: Intent?,
        caller: ComponentCaller
    ) {
        super.onActivityResult(requestCode, resultCode, data, caller)
        if (requestCode == 1024 && resultCode == Activity.RESULT_OK) {
            checkReadCameraPermission()
        }
    }

    private fun checkReadCameraPermission() {
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ) == PackageManager.PERMISSION_DENIED
            ||
            ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.CAMERA
            ) == PackageManager.PERMISSION_DENIED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA
                ),
                1024
            )
        } else {
            jumpMain()
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 1024) {
            jumpMain()
        }
    }

    override fun onTimeout() {
    }
}